//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;
var localUrl = window.location.protocol + "//" + window.location.host + "/wtf"

//全局配置
$.ajaxSetup({
	dataType: "json",
	contentType: "application/json",
	cache: false
});

function hasPermission(permission) {
    if (window.parent.permissions.indexOf(permission) > -1) {
        return true;
    } else {
        return false;
    }
}

//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    return grid.getGridParam("selarrrow");
}

function getSelectedRowsData() {
	var selectedRows = this.getSelectedRows();
	console.log(selectedRows)
	var rowDatas = []
	for (var i = 0; i < selectedRows.length; i++) {
		var rowData = $("#jqGrid").jqGrid("getRowData", selectedRows[i]);
		rowDatas.push(rowData)
	}
	return rowDatas
}

function isEmptyStr(s) {
	if (s == undefined || s == null || s == '') {
		return true
	}
	return false
}
function httpPost(url,bodyParam,successMsg) {
	const options = {
		method: 'POST',
		headers: {
			"Content-type": "application/json; charset=UTF-8",
		},
		body: JSON.stringify(bodyParam)
	}
	fetch(url,options).then(res => res.json()).then(res => {
		//跨域问题：前端端口8080，后端端口9090，导致跨域
		if(res.code != 0){
			window.alert(res.msg)
		} else {
			window.alert(successMsg)
		}
	})
}

function httpGet(url,successMsg) {
	fetch(url).then(res => res.json()).then(res => {
		//跨域问题：前端端口8080，后端端口9090，导致跨域
		if(res.code != 0){
			window.alert(res.msg)
		} else {
			window.alert(successMsg)
		}
	})
}
function dsList(ds) {
	let dsUrl = "http://localhost:8003/wtf/ds/list"
	if(!isEmptyStr(ds)){
		dsUrl = dsUrl + "?ds=" + ds
	}
	let dsList = []
	fetch(dsUrl).then(res => res.json()).then(res => {
		//跨域问题：前端端口8080，后端端口9090，导致跨域
		if(res.code != 0){
			window.alert(res.msg)
		} else {
			var dataRList = res.data;
			for(var i = 0; i < dataRList.length; i++){
				var ii = dataRList[i];
				dsList.push(ii.ds)
			}
			return dsList;
		}
	})
}